/* fafamtg.p - FIXED ASSETS MAINTENANCE - Depreciation Adjustments          */
/* Copyright 1986 QAD Inc. All rights reserved.                               */
/* $Id:: fafamtg.p 84453 2018-10-16 05:54:05Z aqf                          $: */
/* REVISION: 9.1      LAST MODIFIED: 10/26/99   BY: *N021* Pat Pigatti      */
/* REVISION: 9.1      LAST MODIFIED: 03/24/00   BY: *N08T* Annasaheb Rahane */
/* REVISION: 9.1      LAST MODIFIED: 05/12/00   BY: *M0MN* Shilpa Athalye   */
/* REVISION: 9.1      LAST MODIFIED: 05/15/00   BY: *M0MR* Veena Lad        */
/* REVISION: 9.1      LAST MODIFIED: 05/17/00   BY: *M0LJ* Vihang Talwalkar */
/* REVISION: 9.1      LAST MODIFIED: 07/28/00   BY: *N0BX* Arul Victoria    */
/* REVISION: 9.1      LAST MODIFIED: 08/14/00   BY: *N0L0* Jacolyn Neder    */
/* REVISION: 9.1      LAST MODIFIED: 08/18/00   BY: *M0NL* Abbas Hirkani    */
/* Revision: 1.20.1.13  BY: Kirti Desai  DATE: 05/03/02  ECO: *N1GN*        */
/* Revision: 1.20.1.14  BY: Rajaneesh S. DATE: 05/22/02  ECO: *M1Y9*        */
/* Revision: 1.20.1.15  BY: Mercy Chittilapilly DATE: 03/26/03  ECO: *N26Y* */
/* Revision: 1.20.1.16  BY: Rajesh Lokre DATE: 04/03/03 ECO: *M1RX* */
/* Revision: 1.20.1.18  BY: Paul Donnelly (SB) DATE: 06/26/03 ECO: *Q00C* */
/* Revision: 1.20.1.20  BY: Dorota Hohol       DATE: 09/01/03 ECO: *P0YS* */
/* Revision: 1.20.1.21  BY: Vandna Rohira      DATE: 12/17/04 ECO: *P2Z5* */
/* Revision: 1.20.1.27  BY: Sachin Deshmukh    DATE: 04/05/04 ECO: *P3FJ* */
/* Revision: 1.20.1.28  BY: Austin Xie    DATE: 05/16/06 ECO: *R057* */
/* Revision: 1.20.1.30  BY: Austin Xie    DATE: 05/26/06 ECO: *R05R* */
/* Revision: 1.20.1.30  BY: Austin Xie    DATE: 06/13/06 ECO: *R067* */
/* Revision: 1.20.1.31    BY: Ashim Mishra  DATE: 01/14/10 ECO: *Q3QK* */
/* Revision: 1.20.1.32    BY: Anurag Sharma DATE: 06/21/10 ECO: *Q43L* */
/* Revision: 1.20.1.34    BY: Diyin Zhou    DATE: 07/08/10 ECO: *R21B* */
/* REVISION: 23537     LAST MODIFIED: 10/19/12    BY: ddo                     */
/*-Revision end---------------------------------------------------------------*/
/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/

{us/bbi/mfdeclre.i}
{us/bbi/gplabel.i} /* EXTERNAL LABEL INCLUDE */
{us/gp/gprunpdf.i "mcpl" "p"}

/* Common API Constants and Variables */
{us/mf/mfaimfg.i}

define input-output parameter l_astpost like mfc_logical no-undo.
define input-output parameter l_cost    like fa_puramt   no-undo.

/* --------------------  Define Query  ------------------- */
define query q_faadj_mstr
   for faadj_mstr
   scrolling.

/* -----------------  Standard Variables  ---------------- */
define variable p-status       as character no-undo.
define variable perform-status as character format "x(25)"
   initial "first" no-undo.
define variable p-skip-update  like mfc_logical no-undo.
define variable l-rowid        as rowid     no-undo.
define variable l-delete-it    like mfc_logical no-undo.
define variable l-del-rowid    as rowid     no-undo.
define variable l-top-rowid    as rowid     no-undo.
define variable lines          as integer   initial 10 no-undo.
define variable l-found        like mfc_logical no-undo.
define variable pos            as integer   no-undo.
define variable l-first        like mfc_logical no-undo.
define variable l-error        like mfc_logical no-undo.
define variable l-title        as character no-undo.
define variable l-error-number like msg_nbr no-undo.
define variable l-periods      as integer   no-undo.
define variable l-open-yrper   like fabd_yrper no-undo.
define variable l-numper       as integer   no-undo.

/* ------------------  Button Variables  ----------------- */
define button b-add  label "Add".
define button b-undo label "Undo".
define button b-end  label "End".

/* -------------  Standard Widget Variables  ------------- */
define variable l-focus            as widget-handle no-undo.
define variable w-frame            as widget-handle no-undo.

/* ------------------  Screen Variables  ----------------- */
define shared variable l-fabk-id          as character     format "x(4)" no-undo.
define shared variable l-fa-id            as character     format "x(8)" no-undo.
define variable l-famt-meth        as character     format "x(8)"
   initial """" no-undo.
define variable lUseSalvage        as logical       initial no no-undo.
define variable l-resrv            as integer       format ">>9" initial 0 no-undo.
define variable l-type             as character     format "x(8)" no-undo.
define variable l-err-nbr          as integer       format "->,>>>,>>9" no-undo.
define variable l-last-type        as character     format "x(8)"
   initial """" no-undo.
define variable faadj_amt_fmt      as character     format "x(30)" no-undo.
define variable l-adj-date         as date          format "99/99/9999" no-undo.
define variable l-type-desc        as character     format "x(8)" no-undo.
define variable l-remain-total     as decimal       format "->>>,>>>,>>>,>>9.999"
   no-undo.
define variable l-beg-accdep       like fabd_accamt no-undo.
define variable l-end-accdep       like fabd_accamt no-undo.
define variable l-type-translation as character     format "x(24)" no-undo.
define variable l-valid            like mfc_logical no-undo.
define variable l-suspend          like mfc_logical no-undo.
define variable l-post             like mfc_logical no-undo.

define variable l_amt_fmt          as character     no-undo.
define variable mc-error-number    like msg_nbr no-undo.
define variable rndmthd            like rnd_rnd_mthd no-undo.
define variable l-curr             as character     no-undo.
/* Screen value variable definitions */
define variable svFaadjFamtID      as character     no-undo.
define variable svFaadjYrPer       as character     no-undo.
define variable svFaadjLife        as decimal       no-undo.
define variable svFaadjAmt         as decimal       no-undo.
define variable svFaadjSL          as logical       no-undo.
define variable svTypeDesc         as character     no-undo.

/* Fixed Asset API dataset definition */
{us/fa/fadsfamt.i "reference-only"}

/* Adjustment Types *
 * 1 - Bonus
 * 2 - Basis
 * 3 - Method
 * 4 - Life
 * 5 - Suspend
 * 6 - Reinstate
 * 12 - Salvage
 * 90 - Retire
 * 91 - Units of Production
 * 92 - Split 
 **************************/

/* ------------------  Frame Definition  ----------------- */
/* Added side-labels phrase to frame statement              */
if c-application-mode <> "API" then 
do:
   define frame f-button
      b-add at 1
      b-undo at 10
      b-end at 19
      with no-box overlay three-d side-labels width 28.

   /* CLEAR FRAME REGISTRATION TO TRANSLATE THE FRAME EVERYTIME*/
   clearFrameRegistration(frame f-button:handle).

   /* SET EXTERNAL LABELS */
   setFrameLabels(frame f-button:handle).

   assign
      l-focus             = b-add:handle in frame f-button
      b-add:width         = 8
      b-add:private-data  = "add"
      b-undo:width        = 8
      b-undo:private-data = "undo"
      b-end:width         = 8
      b-end:private-data  = "end".

   define frame f-1
      skip(.4)
      space(2)
      l-type-desc column-label "Type"
      faadj_famt_id
      faadj_life
      faadj_amt
      l-curr
      faadj_yrper
      faadj_sl
      with three-d overlay  3 down scroll 1 width 80.

   /* CLEAR FRAME REGISTRATION TO TRANSLATE THE FRAME EVERYTIME*/
   clearFrameRegistration(frame f-1:handle).

   /* SET EXTERNAL LABELS */
   setFrameLabels(frame f-1:handle).

   run ip-framesetup.

   w-frame = frame f-1:handle.

   {us/bbi/gprun.i ""fafmtut.p""
	   "(w-frame)"}

end. /* c-application-mode <> "API" */
if c-application-mode = "API" then 
do on error undo, return:

   /* Get Handle of API Controller */
   {us/bbi/gprun.i ""gpaigach.p"" "(output ApiMethodHandle)"}

   if not valid-handle(ApiMethodHandle) then 
   do:
      /* API Error */
      {us/bbi/pxmsg.i &MSGNUM=10461 &ERRORLEVEL=4}
      return.
   end.

   /* Get the Fixed Asset API dataset from the controller */
   run getRequestDataset in APIMethodHandle
      (output dataset dsFixedAssetMaster bind).

end. /* c-application-mode = "API" */    
lines = 03.

/* ---------------  Pre Processing Include  -------------- */
{us/fa/falffx.i}  /* FUNCTION TO VALIDATE ESTIMATED LIFE */
{us/gp/gpglefv.i} /* VARIABLES FOR GPGLEF1.P */
{us/gp/gprunpdf.i "fapl" "p"}  /* VARIABLES FOR FA PERSISTENT PROCEDURES */

define buffer last-faadj for faadj_mstr.

for first fa_mstr
   where fa_mstr.fa_domain = global_domain 
   and  fa_id = l-fa-id
   no-lock:
end. /* FOR FIRST fa_mstr */.

if not available fa_mstr
then
   return.

/* REMOVE FIELD LIST SINCE BUFFER COPY IS BEING DONE IN faajblb.p */
for last fab_det
   where fab_det.fab_domain = global_domain
   and fab_fa_id = l-fa-id
   and fab_fabk_id = l-fabk-id
   no-lock use-index fab_resrv:
end. /* FOR LAST fab_det */


for first famt_mstr
   where famt_mstr.famt_domain = global_domain 
   and  famt_id = fab_famt_id
   no-lock:
   l-famt-meth = famt_type.
   lUseSalvage = famt_salv.
end. /* FOR FIRST famt_mstr */

for first fabk_mstr
   where fabk_mstr.fabk_domain = global_domain 
   and  fabk_id = fab_fabk_id
   no-lock:
end. /* FOR FIRST fabk_mstr */

if not available fabk_mstr
then do:
   /* INVALID BOOK CODE */
   {us/bbi/pxmsg.i &MSGNUM=4214 &ERRORLEVEL=4}
   return.
end. /* IF NOT AVAILABLE fabk_mstr */

for first cu_mstr
   where cu_mstr.oid_cu_mstr = fabk_mstr.oid_cu_mstr
   no-lock:
   assign 
      l-curr = cu_mstr.cu_curr.
   {us/gp/gprunp.i "mcpl" "p" "mc-get-rnd-mthd"
    "(input l-curr,
      output rndmthd,
      output mc-error-number)"}
   if mc-error-number <> 0 then 
   do:
      {us/bbi/pxmsg.i &MSGNUM=mc-error-number &ERRORLEVEL=3}
   end.
end.

if not available cu_mstr
then do:
   /* INVALID CURRENCY */
   {us/bbi/pxmsg.i &MSGNUM=16123 &ERRORLEVEL=4}
   return.
end. /* IF NOT AVAILABLE cu_mstr */

for last last-faadj
   where last-faadj.faadj_domain = global_domain
   and faadj_fa_id = l-fa-id
   and faadj_fabk_id = l-fabk-id
   no-lock use-index faadj_fa_id:

   assign
      l-resrv     = faadj_resrv
      l-last-type = faadj_type.
end. /* FOR LAST last-faadj */

/* SET CURRENCY DEPENDENT ROUNDING FORMATS */
for first gl_ctrl
   where gl_ctrl.gl_domain = global_domain 
   no-lock:
end. /* FOR FIRST gl_ctrl */

if rndmthd = ? or rndmthd = '' then
do:
   assign 
      rndmthd = gl_rnd_mthd.
end.

if c-application-mode <> "API" then 
do:
   faadj_amt_fmt = faadj_mstr.faadj_amt:format.

   {us/bbi/gprun.i ""gpcurfmt.p""
   "(input-output faadj_amt_fmt,
     input        rndmthd)"}

   faadj_mstr.faadj_amt:format = faadj_amt_fmt.
end. /* if c-application-mode <> "API" */

/* -------------  END Pre Processing Include  ------------ */

if c-application-mode <> "API" then 
do:

   open query q_faadj_mstr
      for each faadj_mstr
      where faadj_mstr.faadj_domain = global_domain
      and  faadj_fa_id   = l-fa-id
      and   faadj_fabk_id = l-fabk-id
      and   faadj_type < "90"
      use-index faadj_fa_id no-lock.
    
   get first q_faadj_mstr no-lock.

   l_amt_fmt = faadj_amt:format in frame f-1.

   assign
      faadj_amt:format in frame f-1 = l_amt_fmt.  

end.
else 
do:
   assign
      perform-status = "next".
end. /* c-application-mode = "API" */

main-loop:
do while perform-status <> "end" on error undo:

   run ip-query
      (input-output perform-status,
      input-output l-rowid).

   /* -----------------------  Undo  ---------------------- */
   if perform-status = "undo"
   then do:

      perform-status = "delete".
      run ip-lock
         (input-output perform-status).

      perform-status = "first".
      run ip-predisplay.
      if c-application-mode <> "API" then
         clear frame f-1 no-pause.
   end. /* IF perform-status = "undo" */

   /* ---------------------  END Undo  -------------------- */

   /* ----------------------  Display  ---------------------- */
   run ip-predisplay.

   /* ----------------------  Add  ------------------------ */
   
   if (perform-status = "update"
      or  perform-status = "add")
   then do:
      run ip-prompt.

      if perform-status = "undo"
      then do:
         perform-status = "first".
         next main-loop.
      end. /* IF perform-status = "undo" */
      run ip-displayedits.
      if c-application-mode = "API" then
         perform-status = "end".
   end. /* IF (perform-status = "update" */

   /* -----------------------  End  ----------------------- */
   if perform-status = "end"
   then do:
      if c-application-mode <> "API" then
         hide frame f-1 no-pause.
      delete PROCEDURE this-procedure no-error.
      leave.
   end. /* IF perform-status = "end" */

   /* ----------------  Selection Buttons  ---------------- */
   if perform-status <> "first"
   then
      run ip-button
         (input-output perform-status).

   /* -------------  After Strip Menu Include  ------------ */
   if (perform-status = "add"
      or perform-status = "undo")
      and fa_mstr.fa_disp_dt <> ?
   then do:
      /* CANNOT ADJUST- ASSET RETIRED */
      {us/bbi/pxmsg.i &MSGNUM=3201 &ERRORLEVEL=4}
      perform-status = "first".
   end. /* IF (perform-status = "add" ... */
   else
      if perform-status = "undo"
      then do:

         if l-famt-meth = "2"
         then do:
            /* CANNOT UNDO UOP ADJUSTMENTS */
            {us/bbi/pxmsg.i &MSGNUM=2972 &ERRORLEVEL=4}
            perform-status = "first".
         end. /* IF l-famt-meth = "2" */
         else
            if not available faadj_mstr
            then do:
               {us/bbi/pxmsg.i &MSGNUM=3224 &ERRORLEVEL=4}
               perform-status = "first".
            end. /* IF NOT AVAILABLE faadj_mstr */
            else
               if can-find(first last-faadj
                  where last-faadj.faadj_domain = global_domain 
                  and last-faadj.faadj_fa_id = l-fa-id
                  and last-faadj.faadj_fabk_id = l-fabk-id
                  and last-faadj.faadj_resrv > faadj_mstr.faadj_resrv
                  and last-faadj.faadj_type = "92")
               then do:
                  /* CANNOT UNDO - ASSET SPLIT */
                  {us/bbi/pxmsg.i &MSGNUM=3309 &ERRORLEVEL=4}
                  perform-status = "first".
               end. /* IF CAN-FIND(FIRST last-faadj ... */
               else
                  if can-find(first last-faadj
                     where last-faadj.faadj_domain = global_domain 
                     and last-faadj.faadj_fa_id = l-fa-id
                     and last-faadj.faadj_fabk_id = l-fabk-id
                     and last-faadj.faadj_resrv > faadj_mstr.faadj_resrv)
                  then do:
                     /* ONLY THE MOST RECENT ADJUSTMENT CAN BE UNDONE */
                     {us/bbi/pxmsg.i &MSGNUM=3310 &ERRORLEVEL=4}
                     perform-status = "first".
                  end. /* IF CAN-FIND(FIRST last-faadj */
                  else
                     if faadj_migrate = yes
                     then do:
                        /* CANNOT UNDO - MIGRATED ADJUSTMENT */
                        {us/bbi/pxmsg.i &MSGNUM=3063 &ERRORLEVEL=4}
                        perform-status = "first".
                     end. /* IF faadj_migrate = YES */
                     else
                        if can-find(first fabd_det
                           where fabd_det.fabd_domain = global_domain 
                           and fabd_fa_id = l-fa-id
                           and fabd_fabk_id = l-fabk-id
                           and fabd_resrv = faadj_resrv
                           and fabd_post = yes)
                        then do:
                           /* CANNOT UNDO - ADJUSTMENT POSTED */
                           {us/bbi/pxmsg.i &MSGNUM=3223 &ERRORLEVEL=4}
                           perform-status = "first".
                        end. /* IF CAN-FIND(FIRST fabd_det */
                        else
                           if faadj_type = "7"
                           then do:
                              /* CANNOT UNDO - PERIOD ADJUSTMENT CREATED THROUGH UTILITY */
                              {us/bbi/pxmsg.i &MSGNUM=3638 &ERRORLEVEL=4}
                              perform-status = "first".
                           end. /* IF faadj_type = "7" */
      end.  /* ELSE IF perform-status = "undo" */

end. /* DO WHILE perform-status <> "end" */

PROCEDURE ip-prompt:

   define variable l-cnt as integer initial 0 no-undo.
   define buffer fabd-det for fabd_det.
   
   if perform-status = "add"
   then do:
      if c-application-mode <> "API" then
         scroll from-current down with frame f-1.
      l-rowid = ?.
   end. /* IF perform-status = "add" */

   l-first = yes.

   if c-application-mode <> "API" then 
   do:
   
      CASE perform-status:
         when ("add")
         then
            /* ADDING NEW RECORD */
            {us/bbi/pxmsg.i &MSGNUM=1 &ERRORLEVEL=1}
         when ("update")
         then
            /* MODIFYING EXISTING RECORD */
            {us/bbi/pxmsg.i &MSGNUM=10 &ERRORLEVEL=1}
         otherwise
         hide message no-pause.
      END CASE. /* perform-status */
   
      ststatus = stline[2].
      status input ststatus.
   
   end. /* if c-application-mode <> "API" */

   /* BEFORE-ADD-EDIT */
   if l-first
      and perform-status = "add"
   then do:

      for last fab_det
         where fab_det.fab_domain = global_domain
         and fab_fa_id = l-fa-id
         and fab_fabk_id = l-fabk-id
         no-lock use-index fab_resrv:
      end. /* FOR LAST fab_det */

      if available fab_det then 
      do:
         if c-application-mode <> "API" then
            assign
               faadj_famt_id:screen-value = fab_det.fab_famt_id
               faadj_life:screen-value    = string(fab_det.fab_life)
               faadj_amt:screen-value     = "0"
               faadj_sl:screen-value      = "SL"
               svFaadjLife = fab_det.fab_life
               svFaadjFamtID = fab_det.fab_famt_id.
         else
            assign
               svFaadjFamtID = fab_det.fab_famt_id
               svFaadjLife   = fab_det.fab_life
               svFaadjAmt    = 0
               svFaadjSl     = true.
      end.
   end.
   /* BEFORE-ADD-EDIT */

   prompt-for-it:
   repeat:
      l-first = no.

      if c-application-mode <> "API" then
      do:
         assign
            faadj_amt:format in frame f-1 = l_amt_fmt.
         display l-curr with frame f-1.
      end.
      repeat:
         /* FIELD EDIT FOR l-type-desc */
         l-error = no.
         l-cnt = 0.
         if c-application-mode <> "API" then 
         do:
            prompt-for
               l-type-desc
               with frame f-1.

            assign
               l-type-desc.
         end.    
            
         assign
            svTypeDesc = if c-application-mode <> "API" then
                            l-type-desc
                         else 
                            ttFixedAssetBookAdj.adjType. 

         for first fabd-det
            where  fabd-det.fabd_domain   = global_domain
            and    fabd_fa_id             = l-fa-id
            and    fabd_fabk_id           = l-fabk-id
            and    fabd_post              = no
         no-lock:
            if can-find (first faadj_mstr
                            where faadj_mstr.faadj_domain  = global_domain
                            and   faadj_mstr.faadj_fa_id   = fabd-det.fabd_fa_id
                            and   faadj_mstr.faadj_fabk_id = fabd-det.fabd_fabk_id
                            and   faadj_mstr.faadj_yrper   > fabd-det.fabd_yrper)
            then do:
               {us/bbi/pxmsg.i &MSGNUM=15161 &ERRORLEVEL=3}
               l-error = yes.
            end. /* IF can-find faadj_mstr */
         end. /* FOR FIRST fabd-det */

         {us/gp/gplngv.i
            &file = ""faadj_mstr""
            &field = ""faadj_type""
            &mnemonic = "svTypeDesc"
            &isvalid = l-valid}

         if l-valid = no
         then do:
            /* INVALID TYPE */
            {us/bbi/pxmsg.i &MSGNUM=4211 &ERRORLEVEL=3}
            l-error = yes.
         end.
         else 
         do:
            {us/gp/gplnga2n.i
               &file = ""faadj_mstr""
               &field = ""faadj_type""
               &mnemonic = "svTypeDesc"
               &code = l-type
               &label = l-type-desc}

            if (l-type < "1")
               or (l-type > "89")
               or (l-type = "7" )
            then do:
               /* Invalid type */
               {us/bbi/pxmsg.i &MSGNUM=4211 &ERRORLEVEL=3}
               l-error = yes.
            end. /* IF (l-type < "1") */
         end. /* ELSE DO */

         if l-error = no
            /* Non basis adjustment is not allowed for UOP */
            and (l-famt-meth = "2"
            and l-type <> "2")
         then do:
            /* Invalid type */
            {us/bbi/pxmsg.i &MSGNUM=4211 &ERRORLEVEL=3}
            l-error = yes.
         end. /* IF l-error */

         /* Salvage adjustment is not allowed if the method of this book
          * is not marked as "Use Salvage"
          */
         if l-error = no and l-type = "12" and not lUseSalvage then 
         do:
            {us/bbi/pxmsg.i &MSGNUM=11153 &ERRORLEVEL=3}
            l-error = yes.
         end.

         if  l-error = no
            and l-famt-meth <> "2"
         then do:

            if l-error = no
            then do:

               if l-last-type = "5"
                  and l-type <> "6"
               then do:
                  /* Cannot adjust - depreciation suspended */
                  {us/bbi/pxmsg.i &MSGNUM=3226 &ERRORLEVEL=3}
                  l-error = yes.
               end. /* IF l-last-type = "5" ... */

            end. /* IF l-error = NO */

            if l-error = no
               and l-type <> "6"
            then do:

               /* Validate against open period */
               for each fabd-det
                  where fabd-det.fabd_domain = global_domain 
                  and  fabd_fa_id = l-fa-id
                  and  fabd_fabk_id = l-fabk-id
                  no-lock
                  break by fabd_yrper:

                  if first-of(fabd_yrper)
                  then do:

                     if fabd_post = no
                     then do:

                        l-cnt = l-cnt + 1.
                        if l-cnt > 1
                        then
                           leave.
                     end. /* IF fabd_post = NO */
                  end.  /* IF FIRST-OF ... */
               end.  /* FOR EACH fabd-det ... */

               if l-cnt = 0
               then do:
                  /* Cannot adjust - asset fully depreciated */
                  {us/bbi/pxmsg.i &MSGNUM=3214 &ERRORLEVEL=3}
                  l-error = yes.
               end. /* IF l-cnt = 0 */
               else
                  /* Invalid if not life or suspend adjustment */
                  if l-cnt = 1
                     and integer(l-type) < integer("4")
                  then do:
                     /* Cannot adjust - final period */
                     {us/bbi/pxmsg.i &MSGNUM=3227 &ERRORLEVEL=3}
                     l-error = yes.
                  end. /* IF l-cnt = 1 */
            end.  /* IF l-error = no */
         end.  /* IF non UOP method */
         if l-error
         then do:
            if c-application-mode <> "API" then 
            do:
               next-prompt l-type-desc with frame f-1.
               next.
            end.
            else 
            do:
               return.
            end.
         end. /* IF l-error */
         /* END FIELD EDIT FOR l-type-desc */

         leave.
      end. /* REPEAT */

      if keyfunction(lastkey) = "end-error"
      then do:
         perform-status = "undo".
         return no-apply.
      end. /* IF KEYFUNCTION(LASTKEY) */

      repeat:
         
         if c-application-mode <> "API" then
            prompt-for
               faadj_mstr.faadj_famt_id
               when (l-type = "3")
               with frame f-1.

         if l-type = "3" then
            assign
               svFaadjFamtId = if c-application-mode <> "API" then
                               input frame f-1 faadj_famt_id
                            else
                               ttFixedAssetBookAdj.adjMethod. 

         /* FIELD EDIT FOR faadj_famt_id */
         l-error = no.
         if l-type = "3"
         then do:

            for first famt_mstr
               where famt_domain = global_domain
               and famt_id = svFaadjFamtId no-lock:
            end. /* FOR FIRST famt_mstr */

            if not available famt_mstr
            then do:
               /* Invalid depreciation method */
               {us/bbi/pxmsg.i &MSGNUM=4200 &ERRORLEVEL=3}
               l-error = yes.
            end. /* IF NOT AVAILABLE famt_mstr */
            else
               if famt_active = no
               then do:
                  {us/bbi/pxmsg.i &MSGNUM=3220 &ERRORLEVEL=3}
                  /* Depreciation Method Inactive */
                  l-error = yes.
               end. /* IF famt_active = NO */
               else
                  if svFaadjFamtId = fab_det.fab_famt_id
                  then do:
                     {us/bbi/pxmsg.i &MSGNUM=3229 &ERRORLEVEL=3}
                     /* Cannot adjust - method is the same */
                     l-error = yes.
                  end. /* IF INPUT FRAME f-1 ... */
                  else
                     if famt_type = "6" then 
                     do:
                        if c-application-mode <> "API" then
                           faadj_life:screen-value = string(famt_elife).
                        else
                           svFaadjLife = famt_elife.
                     end.
         end. /* IF l-type = "3" */
         if l-error
         then do:
            if c-application-mode <> "API" then 
            do:
               next-prompt faadj_famt_id with frame f-1.
               next.
            end.
            else
               return.
         end. /* IF l-error */
         /* END FIELD EDIT FOR faadj_famt_id */

         leave.
      end. /* REPEAT */

      if keyfunction(lastkey) = "end-error"
      then
         next prompt-for-it.

      repeat:
         
         if c-application-mode <> "API" then 
         do:
            prompt-for
               faadj_mstr.faadj_life 
               when (l-type = "4") /* Life */
               with frame f-1.
         end.

         if l-type = "4" then
            assign
               svFaadjLife = if c-application-mode <> "API" then
                               input frame f-1 faadj_life
                            else 
                               ttFixedAssetBookAdj.adjLife. 
         
         /* FIELD EDIT FOR faadj_life */
         l-error = no.
         /* IF TYPE IS CHANGE OF LIFE THEN VALIDATE NEW LIFE */
         if l-type = "4"
         then do:

            if svFaadjLife = fab_det.fab_life
            then do:
               /* CANNOT ADJUST - LIFE IS THE SAME */
               {us/bbi/pxmsg.i &MSGNUM=3228 &ERRORLEVEL=3}
               l-error = yes.
            end. /* IF INPUT FRAME f-1 */
            else 
            do:
               assign
                  l-err-nbr = f-validate-life(fab_famt_id, svFaadjLife)
                  l-error   = l-err-nbr > 0.

               if l-err-nbr > 0
               then do:
                  {us/bbi/pxmsg.i &MSGNUM=l-err-nbr &ERRORLEVEL=3}
               end. /* IF l-err-nbr > 0 */
            end. /* ELSE DO */
         end.  /* IF INPUT FRAME f-1 faadj_type = "4" */


         l-periods = 0.

         {us/gp/gprunp.i "fapl" "p" "fa-get-tot-numper"
                   "(input  string(fab_det.fab_fa_id),
                     input  fab_det.fab_fabk_id,
                     input  fab_det.fab_resrv,
                     output l-periods,
                     output l-open-yrper)"}

         {us/gp/gprunp.i "fapl" "p" "fa-get-perinyr"
                   "(input  string(l-open-yrper),
                     input  fabk_mstr.fabk_calendar,
                     output l-numper)"}

         if l-periods > (svFaadjLife * l-numper)
         then do:
            /* Flat Rate Can have Zero Life, skip such assets*/
            if (l-famt-meth = "5"
                or l-famt-meth = "2")
               and svFaadjLife = 0
            then .
            else 
            do:
               l-error = yes.
               /* DEPRECIATION HAS BEEN POSTED FOR ADJUSTMENT PERIOD */
               {us/bbi/pxmsg.i &MSGNUM=3225 &ERRORLEVEL=3}
            end.
         end. /* IF l-periods > faadj_mstr.faadj_life * l-numper */

         if l-error
         then do:
            if c-application-mode <> "API" then 
            do:
               next-prompt faadj_life with frame f-1.
               next.
            end.
            else
               return.
         end. /* IF l-error */

         /* END FIELD EDIT FOR faadj_life */

         leave.
      end. /* REPEAT */

      if keyfunction(lastkey) = "end-error"
      then
         next prompt-for-it.

      repeat:
         svFaadjAmt = 0.
         if c-application-mode <> "API" then 
         do:
            prompt-for
               faadj_mstr.faadj_amt
               when (l-type   = "1"
               or l-type = "2"
               or l-type = "12")
               faadj_mstr.faadj_yrper
               when (l-type = "1" or l-type = "5" or l-type = "6")
               faadj_mstr.faadj_sl
               when (l-type = "2" or
               l-type = "3" or
               l-type = "4" or
               l-type = "12")
               with frame f-1.
         end.

         if l-type = "1" or l-type = "2" or l-type = "12" then
            assign
               svFaadjAmt = if c-application-mode <> "API" then
                              input frame f-1 faadj_amt
                           else 
                              ttFixedAssetBookAdj.adjAmount. 

         if l-type = "1" or l-type = "5" or l-type = "6" then
            assign
               svFaadjYrPer = if c-application-mode <> "API" then
                              input frame f-1 faadj_yrper
                           else 
                              ttFixedAssetBookAdj.adjYrPer. 

         if l-type = "2" or l-type = "3" or l-type = "4" or l-type = "12" then
            assign
               svFaadjSL = if c-application-mode <> "API" then
                              input frame f-1 faadj_sl
                           else
                              ttFixedAssetBookAdj.adjSL. 

         /* FIELD EDIT FOR faadj_amt */
         l-error = no.

         if l-type = "2"    /*Basis*/
            and svFaadjAmt = 0
         then do:
            /* ZERO NOT ALLOWED */
            {us/bbi/pxmsg.i &MSGNUM=317 &ERRORLEVEL=3}
            l-error = yes.
         end. /* IF l-type = "2" ... */

         if l-type = "1"  /* Bonus */
            and svFaadjAmt <= 0
         then do:
            /* BONUS ADJUSTMENT AMOUNT MUST BE GREATER THAN ZERO */
            {us/bbi/pxmsg.i &MSGNUM=4077 &ERRORLEVEL=3}
            l-error = yes.
         end. /* IF l-type = "1" AND INPUT ... */

         if l-type = "12" /*Salvage*/
            and svFaadjAmt < 0
         then do:
            /* SALVAGE ADJUSTMENT AMOUNT CANNOT BE NEGATIVE */
            {us/bbi/pxmsg.i &MSGNUM=11152 &ERRORLEVEL=3}
            l-error = yes.
         end. /* IF l-type = "12" AND INPUT ... */

         if l-error = no
         then do:
            /* CHECK CURRENCY DEPENDENT ROUNDING FORMAT */
            {us/bbi/gprun.i ""gpcurval.p""
               "(input  svFaadjAmt,
                 input  rndmthd,
                 output l-err-nbr)"}

            assign
               l-error   = l-err-nbr <> 0
               l-err-nbr = 0.
         end. /* IF l-error = NO */

         if l-error = no
            and l-type = "1"
         then do:
            for last fabd-det
               where fabd-det.fabd_domain = global_domain
               and fabd_fa_id = fab_fa_id
               and fabd_fabk_id = fab_fabk_id
               use-index fabd_fa_id no-lock:
            end. /* FOR LAST fabd-det */

            {us/gp/gprunp.i "fapl" "p" "fa-get-accdep"
               "(input  fab_fa_id,
                 input  fab_fabk_id,
                 input  svFaadjYrper,
                 output l-beg-accdep)"}

            {us/gp/gprunp.i "fapl" "p" "fa-get-accdep"
               "(input  fab_fa_id,
                 input  fab_fabk_id,
                 input  fabd-det.fabd_yrper,
                 output l-end-accdep)"}

            l-remain-total = l-end-accdep - l-beg-accdep.

            if l-remain-total < svFaadjAmt
            then do:
               /* Bonus amount exceeds remaining depreciations */
               {us/bbi/pxmsg.i &MSGNUM=3216 &ERRORLEVEL=3}
               l-error = yes.
            end. /* IF l-remain-total < INPUT FRAME f-1 faadj_amt */
         end.  /* IF l-error = NO AND l-type = "1" */
         if l-error = no
            and (l-type = "2" or l-type = "12")
         then do:
            {us/bbi/gprun.i ""faajbla.p""
               "(buffer fa_mstr,
                 buffer fab_det,
                 input  svFaadjAmt,
                 input  l-type,
                 output l-err-nbr)"}

            if l-err-nbr <> 0
            then do:
               {us/bbi/pxmsg.i &MSGNUM=l-err-nbr &ERRORLEVEL=3}
               l-error = yes.
            end. /* IF l-err-nbr <> 0 */

         end. /* IF l-error = NO AND l-type = "2" */

         if l-error
         then do:
            if c-application-mode <> "API" then 
            do:
               next-prompt faadj_amt with frame f-1.
               next.
            end.
            else
               return.
         end. /* IF l-error */
         /* END FIELD EDIT FOR faadj_amt */

         /* FIELD EDIT FOR faadj_yrper */
         l-error = no.
         if l-type = "1"
            or l-type = "5"
            or l-type = "6"
         then do:

            /* CHECK IF YEAR PERIOD ENTERED IS A VALID ENTRY */
            {us/gp/gprunp.i "fapl" "p" "fa-chk-input-yrper"
            "(input  svFaadjYrper,
                 output l-error-number)"}

            if l-error-number <> 0
            then do:
               l-error = yes.
               /*  40: BLANK NOT ALLOWED */
               /* 495: INVALID PERIOD    */
               {us/bbi/pxmsg.i &MSGNUM=l-error-number &ERRORLEVEL=3}
               if c-application-mode <> "API" then 
               do:
                  next-prompt
                     faadj_yrper
                     with frame f-1.
                  next.
               end.
               else
                  return.
            end. /* IF l-error-number <> 0 */

            {us/gp/gprunp.i "fapl" "p" "fa-get-suspend"
               "(input  fab_fa_id,
                 input  fab_fabk_id,
                 input  svFaadjAmt,
                 output l-suspend)"}

            {us/gp/gprunp.i "fapl" "p" "fa-get-post"
               "(input  fab_fa_id,
                 input  fab_fabk_id,
                 input  svFaadjYrPer,
                 output l-post)"}

            /* FIND THE START DATE FOR A GIVEN YEAR AND PERIOD */
            {us/gp/gprunp.i "fapl" "p" "fa-conv-per-to-date"
               "(input  fabk_mstr.fabk_calendar,
                 input  svFaadjYrper,
                 output l-adj-date,
                 output l-error-number)"}

            if l-error-number <> 0
            then do:
               l-error = yes.
               /* DATE NOT WITHIN A VALID PERIOD */
               {us/bbi/pxmsg.i &MSGNUM=l-error-number &ERRORLEVEL=3}
            end. /* IF l-error-number <> 0 */

            if fabk_calendar = ""
               and l-error      = no
            then do:
               {us/bbi/gprun.i ""gpglef1.p""
                  "(input ""FA"",
                    input fa_entity,
                    input l-adj-date,
                    output gpglef_result,
                    output gpglef_msg_nbr)"}

               if gpglef_result > 0
               then do:
                  {us/bbi/pxmsg.i &MSGNUM=gpglef_msg_nbr &ERRORLEVEL=3}
                  l-error = yes.
               end. /* IF gpglef_result > 0 */
            end.  /* IF fabk_calendar = "" */

            if l-error = no
            then do:

               for first fabd-det
                  where fabd-det.fabd_domain = global_domain 
                  and  fabd_fa_id = l-fa-id
                  and  fabd_fabk_id = l-fabk-id
                  use-index fabd_fa_id:
               end. /* FOR FIRST fabd-det */

               if available fabd-det
                  and svFaadjYrper < fabd_yrper
               then do:
                  /* Cannot adjust - period less than beginning depreciation period */
                  {us/bbi/pxmsg.i &MSGNUM=3218 &ERRORLEVEL=3}
                  l-error = yes.
               end. /* IF AVAILABLE fabd-det */
            end.  /* IF l-error = NO */

            if l-error = no
            then do:

               for last fabd-det
                  where fabd-det.fabd_domain = global_domain
                  and  fabd_fa_id = fab_fa_id
                  and  fabd_fabk_id = fab_fabk_id
                  use-index fabd_fa_id no-lock:
               end. /* FOR LAST fabd-det */

               if l-type <> "6"
                  and svFaadjYrper > fabd_yrper
               then do:
                  /* Adjustment date later than depreciation end date */
                  {us/bbi/pxmsg.i &MSGNUM=3217 &ERRORLEVEL=3}
                  l-error = yes.
               end. /* IF l-type <> 6 */
            end.  /* IF l-error = NO */

            if l-error = no
               and l-type = "6"
            then do:

               run p_err_messg.

            end. /* IF l-error = no ... */
            else
               if l-error = no
                  and l-type <> "6"
               then
                  if l-suspend = yes
                     then 
                  do:
                     /* Cannot adjust - depreciation suspended */
                     {us/bbi/pxmsg.i &MSGNUM=3226 &ERRORLEVEL=3}
                     l-error = yes.
                  end. /* IF l-suspend = YES */
                  else
                     if l-post
                     then do:
                        /* Cannot adjust - depreciation posted */
                        {us/bbi/pxmsg.i &MSGNUM=3222 &ERRORLEVEL=3}
                        l-error = yes.
                     end. /* IF l-post */
                     else
                        CASE l-type:
                           when "1"
                           then 
                              do:

                                 l-cnt = 0.

                                 for each fabd-det
                                    where fabd-det.fabd_domain = global_domain 
                                    and fabd_fa_id = l-fa-id
                                    and fabd_fabk_id = l-fabk-id
                                    and fabd_yrper > svFaadjYrper
                                    no-lock
                                    break by fabd_yrper:

                                    if first-of(fabd_yrper)
                                    then do:

                                       if fabd_post = yes
                                       then
                                          next.
                                       l-cnt = l-cnt + 1.
                                       leave.
                                    end.  /* IF FIRST-OF ... */
                                 end.  /* FOR EACH fabd-det ... */

                                 if l-cnt = 0
                                 then do:
                                    /* Cannot adjust - final period */
                                    {us/bbi/pxmsg.i &MSGNUM=3227 &ERRORLEVEL=3}
                                    l-error = yes.
                                 end. /* IF l-cnt = 0 */

                              end.  /* when "1" */
                           when "5"
                           then 
                              do:

                                 define buffer b-faadj for faadj_mstr.

                                 if can-find(b-faadj
                                    where b-faadj.faadj_domain = global_domain
                                    and faadj_fa_id = l-fa-id
                                    and faadj_fabk_id = l-fabk-id
                                    and faadj_type = "5"
                                    and faadj_yrper >= svFaadjYrper)
                                 then do:
                                    /* Suspend already happened in a later date */
                                    {us/bbi/pxmsg.i &MSGNUM=3215 &ERRORLEVEL=3}
                                    l-error = yes.
                                 end. /* IF CAN_FIND(b-faadj */
                              end. /* WHEN "5" */

                        END CASE.

         end.  /* if "1" or "5" or "6" */

         else
            if l-type = "2"
               or l-type = "3"
               or l-type = "4"
               or l-type = "12"
            then do:
               for first fabd-det
                  where fabd-det.fabd_domain = global_domain 
                  and  fabd_fa_id = l-fa-id
                  and  fabd_fabk_id = l-fabk-id
                  and  fabd_post = no
                  and  fabd_suspend = no
                  no-lock
                  use-index fabd_fa_id:
               end. /* FOR FIRST fabd-det */

               if available fabd-det then 
               do:
                  if c-application-mode <> "API" then
                     faadj_mstr.faadj_yrper:screen-value = fabd_yrper.
                  svFaadjYrper = fabd_yrper.
               end.
            end. /* IF l-type = "2" ... */

         if l-error
         then do:
            if c-application-mode <> "API" then 
            do:
               next-prompt faadj_yrper with frame f-1.
               next.
            end.
            else
               return.
         end. /* IF l-error */

         /* END FIELD EDIT FOR faadj_yrper */

         leave.
      end. /* REPEAT */

      if keyfunction(lastkey) = "end-error"
      then
         next prompt-for-it.
      
      leave.
   end. /* prompt-for-it */

   if keyfunction(lastkey) = "end-error"
   then do:

      if perform-status = "add"
      then do:
         /* RECORD NOT ADDED */
         {us/bbi/pxmsg.i &MSGNUM=1304 &ERRORLEVEL=1}
         
         if c-application-mode <> "API" then
            get first q_faadj_mstr no-lock.

         if available faadj_mstr
         then
            assign
               perform-status = "first"
               l-rowid        = rowid(faadj_mstr).
         else
            assign
               perform-status = ""
               l-rowid        = ?.

         if c-application-mode <> "API" then
            clear frame f-1 no-pause.
      end. /* IF perform-status = "add" */
      else
         if available faadj_mstr
         then
            assign
               perform-status = ""
               l-rowid        = rowid(faadj_mstr).
      return.
   end. /* IF KEYFUNCTION(LASTKEY) = "end-error" */
   
   run ip-lock
      (input-output perform-status).

END PROCEDURE. /* ip-prompt */

/* -----------------------  Locking  ----------------------- */
PROCEDURE ip-lock:

   define input-output parameter perform-status as character no-undo.
   
   if perform-status = "add"
      or perform-status = "update"
      or perform-status = "delete"
   then
   ip-lock:
   do transaction on error undo:
      assign
         pos         = 0
         l-delete-it = yes.

      if c-application-mode <> "API" then 
      do:
         if available faadj_mstr
         then
            get current q_faadj_mstr no-lock.
   
         if available faadj_mstr
            and current-changed faadj_mstr
         then do:
   
            l-delete-it = yes.
            /* RECORD HAS BEEN MODIFIED SINCED YOU EDITED IT SAVE ANYWAY */
            {us/bbi/pxmsg.i &MSGNUM=2042 &ERRORLEVEL=1 &CONFIRM=l-delete-it
                        &CONFIRM-TYPE='LOGICAL'}
   
            if l-delete-it = no
            then do:
               hide message no-pause.
               run ip-displayedits.
               return.
            end. /* IF l-delete-it = NO */
   
            hide message no-pause.
         end. /* IF AVAILABLE faadj_mstr */
      end. /* if c-application-mode <> "API" */

      if available faadj_mstr
      then
      tran-lock:
      do while perform-status <> "":

         if c-application-mode <> "API" then
            get current q_faadj_mstr exclusive-lock no-wait.
         else
            find current faadj_mstr exclusive-lock no-wait.

         if locked faadj_mstr
         then do:

            if pos < 3
            then do:
               pos = pos + 1.
               pause 1 no-message.
               next tran-lock.
            end. /* IF pos < 3 */

            perform-status = "error".
         end. /*  IF LOCKED faadj_mstr */

         leave.
      end. /*while*/

      if (perform-status = "update"
         or  p-status       = "update")
         and l-delete-it    = yes
      then
         run ip-update
            (input-output perform-status).

      if perform-status = "delete"
      then
         run ip-delete
            (input-output perform-status).
			
      if perform-status = "add"
      then
         run ip-add
               (input-output perform-status).			

      if l-error
      then
         undo ip-lock, leave.
      /* ------------  END /* LOCKED-EDITS */  ----------- */

      if available faadj_mstr then 
      do:
         if c-application-mode <> "API" then
            get current q_faadj_mstr no-lock.
         else
            find current faadj_mstr no-lock.
      end.

   end. /* IF perform-status = "add" */

END PROCEDURE. /* ip-lock */

PROCEDURE ip-update:

   define input-output parameter perform-status as character no-undo.

   if p-status = "update"
   then
      p-status = "".

   run ip-assign
      (input-output perform-status).

   perform-status = "open".

   run ip-query
      (input-output perform-status,
      input-output l-rowid).

   if c-application-mode <> "API" then 
   do:
      
      reposition q_faadj_mstr to rowid l-rowid no-error.

      get next q_faadj_mstr no-lock.

      assign
         perform-status = "first"
         l-rowid        = rowid(faadj_mstr).
   
   end. /* if c-application-mode <> "API" */

   return.

END PROCEDURE. /* ip-update */

PROCEDURE ip-add:

   define input-output parameter perform-status as character no-undo.

   create faadj_mstr. 
   faadj_mstr.faadj_domain = global_domain.
   run ip-assign
      (input-output perform-status).

   if c-application-mode <> "API" then
      perform-status = "open".

   /* UPDATE THE POST FLAG AND AMOUNT FOR BASIS ADJUSTMENT */
   if faadj_mstr.faadj_type = "2" and
      fabk_mstr.fabk_post
   then do:
      l_astpost = yes.

      /* GET THE DELTA OF ADJUSTED COST */
      run ip_getcostdiff
         (input faadj_mstr.faadj_fa_id,
         input faadj_mstr.faadj_fabk_id,
         input faadj_mstr.faadj_resrv,
         input yes,
         input-output l_cost).

   end. /* IF faadj_mstr.faadj_type = "2" */

   if not (c-application-mode = "API" and perform-status = "ADD") then
      run ip-query
         (input-output perform-status,
         input-output l-rowid).

   if c-application-mode <> "API"  then 
   do:
      reposition q_faadj_mstr to rowid l-rowid no-error.
      get next q_faadj_mstr no-lock.
      assign
         perform-status = "first"
         l-rowid        = rowid(faadj_mstr).
   end.

   return.

END PROCEDURE. /* ip-add */

PROCEDURE ip-delete:

   define input-output parameter perform-status as character no-undo.

   if c-application-mode <> "API" then 
   do:
      l-delete-it = no.
      /* Please confirm delete */
      {us/bbi/pxmsg.i &MSGNUM=11 &ERRORLEVEL=1 &CONFIRM=l-delete-it}
   end.
   else
      l-delete-it = yes.
   
   CASE l-delete-it:
      when yes
   then do:
            /* UPDATE THE POST FLAG AND AMOUNT FOR BASIS ADJUSTMENT */
            if faadj_mstr.faadj_type = "2"and
               fabk_mstr.fabk_post
            then do:
               l_astpost = yes.
               /* GET THE DELTA OF ADJUSTED COST */
               run ip_getcostdiff
                  (input l-fa-id,
                  input l-fabk-id,
                  input faadj_mstr.faadj_resrv,
                  input no,
                  input-output l_cost).

            end. /* IF faadj_mstr.faadj_type = "2" */

            if c-application-mode <> "API" then
               hide message no-pause.
            /* CHANGED SECOND AND THIRD INPUT PARAMETERS FROM */
            /* fab_det.fab_fa_id AND fab_det.fab_fabk_id TO   */
            /* l-fa-id AND l-fabk-id RESPECTIVELY             */
            {us/bbi/gprun.i ""faudbl.p"" "(buffer faadj_mstr,
                                 input  l-fa-id,
                                 input  l-fabk-id)"}
          
            if available faadj_mstr
            then
               delete faadj_mstr.

            for last fab_det
               where fab_det.fab_domain = global_domain 
               and  fab_fa_id = l-fa-id
               and  fab_fabk_id = l-fabk-id
               no-lock
               use-index fab_resrv:
            end. /* FOR LAST fab_det */

            if c-application-mode <> "API" then 
            do:

               clear frame f-1 no-pause.
               get next q_faadj_mstr no-lock.

               if available faadj_mstr
               then
                  assign
                     perform-status = "first"
                     l-rowid        = rowid(faadj_mstr)
                     l-resrv        = faadj_resrv
                     l-last-type    = faadj_type.
               else 
               do:
                  get prev q_faadj_mstr no-lock.
                  if available faadj_mstr
                  then
                     assign
                        perform-status = "first"
                        l-rowid        = rowid(faadj_mstr)
                        l-resrv        = faadj_resrv
                        l-last-type    = faadj_type.
                  else
                     assign
                        perform-status = "first"
                        l-rowid        = rowid(faadj_mstr)
                        l-resrv        = 0
                        l-last-type    = "".
               end. /* ELSE DO */

               /* RECORD DELETED */
               {us/bbi/pxmsg.i &MSGNUM=22 &ERRORLEVEL=1}

            end. /* if c-application-mode <> "API" */
            else
               assign
                  perform-status = "next".

            return.
         end. /* WHEN YES */
      
      otherwise
      run ip-displayedits.

   END CASE.

END PROCEDURE. /* ip-delete */

PROCEDURE ip-assign:

   define input-output parameter perform-status as character no-undo.

   define buffer bFaadj for faadj_mstr.
   define buffer bfa_mstr for fa_mstr.
   define buffer bfabk_mstr for fabk_mstr.
   define buffer bfad_det for fad_det.
   define variable delta_cost like fa_mstr.fa_puramt no-undo.
   /* Get available adjustment reverse number */
   for each bFaadj no-lock
      where bFaadj.faadj_domain  = global_domain
      and bFaadj.faadj_fa_id = fa_mstr.fa_id:
      if bFaadj.faadj_resrv >= l-resrv
      then l-resrv = bfaadj.faadj_resrv.
   end.

   do with frame f-1:
      assign
         faadj_mstr.faadj_fa_id      = l-fa-id
         faadj_mstr.faadj_resrv      = l-resrv + 1
         faadj_mstr.faadj_fabk_id    = l-fabk-id
         faadj_mstr.faadj_mod_userid = global_userid
         faadj_mstr.faadj_mod_date   = today
         faadj_mstr.faadj_life       = svFaadjLife
         faadj_mstr.faadj_amt        = svFaadjAmt
         faadj_mstr.faadj_yrper      = svFaadjYrPer
         faadj_mstr.faadj_sl         = svFaadjSL
         faadj_mstr.faadj_famt_id    = svFaadjFamtId
         faadj_mstr.faadj_type       = l-type
         l-rowid                     = rowid(faadj_mstr).

      if recid(faadj_mstr) = -1 then .      
      
      /* Once primary book cost or salvage has been changed, change it in the header also */
      for first bfabk_mstr where bfabk_mstr.fabk_domain = global_domain and
        bfabk_mstr.fabk_id = l-fabk-id and
        bfabk_mstr.fabk_primary = yes and
        (l-type = "2" /* BASIS */ or 
         l-type = "12" /* Salvage */ )
        no-lock:
            
        for first bfa_mstr 
            where bfa_mstr.fa_domain = global_domain and
                  bfa_mstr.fa_id     = l-fa-id
            exclusive-lock:
            
            if l-type = "2" /* BASIS */
            then do:               
                assign
                    delta_cost          = svFaadjAmt - bfa_mstr.fa_puramt 
                    bfa_mstr.fa_puramt  = svFaadjAmt. 
                
                for first bfad_det
                    where bfad_det.fad_domain = global_domain and
                          bfad_det.fad_fa_id  = l-fa-id
                    exclusive-lock:
                    assign bfad_det.fad_puramt = bfad_det.fad_puramt + delta_cost.     
                end.  
            end.  
            if l-type = "12" /* Salvage */
            then do:
                assign bfa_mstr.fa_salvamt = svFaadjAmt.                    
            end.                           
        end.           
      end.

      /* --------  /* AFTER-ASSIGN-AUDIT-INCLUDE */  ------- */
      /* No UI in called program, no API Version required */
      {us/bbi/gprun.i ""faajblb.p""  
         "(buffer fa_mstr,
           buffer fab_det,
           buffer faadj_mstr,
           output l-err-nbr)"}

      if l-err-nbr = 0
      then do:

         for last fab_det
            where fab_det.fab_domain = global_domain 
            and  fab_fa_id = l-fa-id
            and  fab_fabk_id = l-fabk-id
            no-lock
            use-index fab_resrv:
         end. /* FOR LAST fab_det */

         assign
            l-resrv     = faadj_resrv
            l-last-type = faadj_type.
      end. /* IF l-err-nbr = 0 */
      else 
      do:
         {us/bbi/pxmsg.i &MSGNUM=l-err-nbr &ERRORLEVEL=4}
         l-error = yes.
         if c-application-mode <> "API" then
            scroll from-current up with frame f-1.
      end. /* ELSE DO */
   /* ------  END /* AFTER-ASSIGN-AUDIT-INCLUDE */  ----- */

   end. /* DO WITH FRAME f-1 */

END PROCEDURE. /* ip-assign */

PROCEDURE ip-predisplay:

   if (perform-status = ""
      or perform-status = "update")
      and available faadj_mstr
   then
   display-loop:
   do:

      if perform-status = ""
      then do:
         if c-application-mode <> "API" then
            clear frame f-1 all no-pause.
         l-top-rowid = rowid(faadj_mstr).
      end. /* IF perform-status = "" */

      read-loop:
      do pos = 1 to lines:
         if perform-status = "" and c-application-mode <> "API"
         then do:
            if session:display-type = "gui"
            then
               assign
                  l-type-desc:bgcolor = 8
                  l-type-desc:fgcolor = 0.
            else
               color display normal l-type-desc
                  with frame f-1.
         end. /* IF perform-status = "" */

         run ip-displayedits.

         if perform-status = "update"
         then
            leave display-loop.

         if c-application-mode <> "API" then 
         do:
            if pos < lines
            then
               down with frame f-1.

            get next q_faadj_mstr no-lock.

            if not available faadj_mstr
            then
               leave.
         end. /* if c-application-mode <> "API" */
      end. /* DO pos = 1 to lines */

      run ip-postdisplay.
   end. /* DO */

END PROCEDURE. /* ip-predisplay */

PROCEDURE ip-displayedits:

   if available faadj_mstr
   then do:

      /* DISPLAY-EDITS */
      {us/gp/gplngn2a.i
         &file = ""faadj_mstr""
         &field = ""faadj_type""
         &code = faadj_type
         &mnemonic = l-type-desc
         &label = l-type-translation}
      /* DISPLAY-EDITS */

      if c-application-mode <> "API" then
         assign
            faadj_mstr.faadj_amt:format in frame f-1 = l_amt_fmt.  

      run ip-display.
   end. /* IF AVAILABLE faadj_mstr */

END PROCEDURE. /* ip-displayedits */

PROCEDURE ip-display:

   if c-application-mode <> "API" then 
   do:
      display
         faadj_mstr.faadj_life
         faadj_mstr.faadj_amt
         faadj_mstr.faadj_yrper
         faadj_mstr.faadj_famt_id
         faadj_mstr.faadj_sl
         l-type-desc
         l-curr
         with frame f-1.
   end.
   else 
   do:
      if perform-status <> "end" then
      do:
         if ttFixedAssetBookAdj.adjLife = 0 or ttFixedAssetBookAdj.adjLife = ? then
            ttFixedAssetBookAdj.adjLife   =  faadj_mstr.faadj_life.
         if ttFixedAssetBookAdj.adjYrPer = '' or ttFixedAssetBookAdj.adjYrPer = ? then
            assign ttFixedAssetBookAdj.adjYrPer = faadj_mstr.faadj_yrper.
         if ttFixedAssetBookAdj.adjMethod = '' or ttFixedAssetBookAdj.adjMethod = ? then
            assign ttFixedAssetBookAdj.adjMethod = faadj_mstr.faadj_famt_id.
         if ttFixedAssetBookAdj.adjAmount = 0 or ttFixedAssetBookAdj.adjAmount = ? then
            assign ttFixedAssetBookAdj.adjAmount = faadj_mstr.faadj_amt.
         assign {us/mf/mfaidflt.i ttFixedAssetBookAdj.adjSL faadj_mstr.faadj_sl}.
         if ttFixedAssetBookAdj.adjType = '' or ttFixedAssetBookAdj.adjType = ? then
            assign ttFixedAssetBookAdj.adjType = l-type-desc.
      end.
   end.		      

END PROCEDURE. /* ip-display */

PROCEDURE ip-postdisplay:
   
   if c-application-mode <> "API" then 
   do:

      do pos = 1 to lines:
         if frame-line(f-1) <= 1
         then
            leave.
         up 1 with frame f-1.
      end. /* DO pos = 1 to lines */

      if perform-status = ""
      then do:
         reposition q_faadj_mstr to rowid l-top-rowid no-error.
         get next q_faadj_mstr no-lock.
         l-rowid = l-top-rowid.
         color display message l-type-desc
            with frame f-1.
      end. /* IF perform-status = "" */

   end.

END PROCEDURE. /* ip-postdisplay */

PROCEDURE ip-query:

   define input-output parameter perform-status as character no-undo.
   define input-output parameter l-rowid as rowid no-undo.

   /* Limit API mode */
   if c-application-mode = "API" then 
   do:
      if perform-status = "first" or
         perform-status = "open" or
         perform-status = "prev" or
         perform-status = "find" then
         perform-status = "next".
   end. /* c-application-mode = "API" */

   if perform-status = "first"
   then do:

      if l-rowid <> ?
      then do:

         reposition q_faadj_mstr to rowid l-rowid no-error.
         get next q_faadj_mstr no-lock.
      end. /* IF l-rowid <> ? */

      if not available faadj_mstr
      then
         get first q_faadj_mstr no-lock.

      if available faadj_mstr
      then
         assign
            perform-status = ""
            l-rowid        = rowid(faadj_mstr).
      else 
      do:
         assign
            perform-status    = ""
            l-rowid           = ?
            frame f-1:visible = true.
         return.
      end. /* ELSE DO */

   end. /* IF perform-status = "first" */

   if perform-status = "open"
   then do:

      open query q_faadj_mstr
         for each faadj_mstr
         where faadj_mstr.faadj_domain = global_domain
         and faadj_fa_id = l-fa-id
         and faadj_fabk_id = l-fabk-id
         and faadj_type < "90"
         use-index faadj_fa_id no-lock.

      reposition q_faadj_mstr to rowid l-rowid no-error.
      get next q_faadj_mstr no-lock.

      if available faadj_mstr
      then
         assign
            perform-status = ""
            l-rowid        = rowid(faadj_mstr).
      else 
      do:
         get first q_faadj_mstr no-lock.
         if not available faadj_mstr
         then do:
            assign
               perform-status    = ""
               frame f-1:visible = true.
            return.
         end. /* IF NOT AVAILABLE faadj_mstr */
         else
            assign
               perform-status = ""
               l-rowid        = rowid(faadj_mstr).
      end. /* ELSE DO */

   end. /* IF perform-status = "open" */

   if perform-status = "next"
   then do:

      if c-application-mode <> "API" then 
      do:
      
         get next q_faadj_mstr no-lock.
         if available faadj_mstr
         then do:
            hide message no-pause.
            assign
               l-rowid        = rowid(faadj_mstr)
               perform-status = "next".
   
            if session:display-type = "gui"
            then
               assign
                  l-type-desc:bgcolor = 8
                  l-type-desc:fgcolor = 0.
            else
               color display normal l-type-desc
                  with frame f-1.
            pause 0.  /* MAKES SCROLLING WORK - DON'T REMOVE */
            down 1 with frame f-1.
            run ip-displayedits.
            color display message l-type-desc
               with frame f-1.
         end. /* IF AVAILABLE faadj_mstr */
         else 
         do:
            perform-status = "next".
            /* End of file */
            {us/bbi/pxmsg.i &MSGNUM=20 &ERRORLEVEL=2}
            get last q_faadj_mstr no-lock.
         end. /* ELSE DO */
      end. /* if c-application-mode <> "API" */
      else 
      do:
         /* Get the Fixed Asset Books Adjust record from the API controller */
         run getNextRecord in ApiMethodHandle 
            (input "ttFixedAssetBookAdj").
      
         if return-value = {&RECORD-NOT-FOUND} then 
         do:
            assign
               perform-status = "end"
               l-rowid        = ?.
            return.
         end.

         if ttFixedAssetBookAdj.operation = {&ADD} then
            perform-status = "add".
         else if ttFixedAssetBookAdj.operation = {&MODIFY} then
               perform-status = "update".
            else if ttFixedAssetBookAdj.operation = {&REMOVE} then
                  perform-status = "delete".
               else if ttFixedAssetBooks.operation = {&GET} then
                     perform-status = "".
                  else 
                     perform-status = "end".
            
         if perform-status = "update" and
            ttFixedAssetBookAdj.adjbutton = "ADD" 
         then
            perform-status = "add".

         run ip-displayedits.
      end. /* c-application-mode = "API" */

   end. /* IF perform-status = "next" */

   if perform-status = "prev"
   then do:

      get prev q_faadj_mstr no-lock.
      if not available faadj_mstr
      then do:
         {us/bbi/pxmsg.i &MSGNUM=21 &ERRORLEVEL=2} /* BEGINNING OF FILE */
         get first q_faadj_mstr no-lock.
         return.
      end. /* IF NOT AVAILABLE faadj_mstr */

      hide message no-pause.
      l-rowid = rowid(faadj_mstr).
      if session:display-type = "gui"
      then
         assign
            l-type-desc:bgcolor = 8
            l-type-desc:fgcolor = 0.
      else
         color display normal l-type-desc
            with frame f-1.

      pause 0.  /* MAKES SCROLLING WORK - DON'T REMOVE */
      up 1 with frame f-1.
      run ip-displayedits.
      color display message l-type-desc
         with frame f-1.
   end. /* IF perform-status = "prev" */

   if perform-status = "update"
      or perform-status = "delete"
   then do:

      if c-application-mode <> "API" then 
      do:
         get current q_faadj_mstr no-lock.
         if not available faadj_mstr
         then do:
            hide message no-pause.
            /* Record not found */
            {us/bbi/pxmsg.i &MSGNUM=1310 &ERRORLEVEL=3}
            perform-status = "".
         end. /* IF NOT AVAILABLE faadj_mstr */
      end. /* c-application-mode <> "API" */
      else 
      do:
         find first faadj_mstr where faadj_domain = global_domain 
            and faadj_fa_id = fa_mstr.fa_id  
            and faadj_fabk_id = ttFixedAssetBookAdj.fabFabkId
            no-lock no-error.
         if not available faadj_mstr then 
         do:
            {us/bbi/pxmsg.i &MSGNUM=1310 &ERRORLEVEL=3}
            perform-status = "end".
         end. /* c-application-mode = "API" */         
      end.

   end. /* IF perform-status = "update" */
END PROCEDURE. /* ip-query */

PROCEDURE ip-framesetup:

   if session:display-type = "gui"
   then
      frame f-1:row = 4.5.
   else
      frame f-1:row = 4.

   assign
      frame f-1:box           = true
      frame f-1:centered      = true
      frame f-1:title         = (getFrameTitle("DEPRECIATION_ADJUSTMENTS",34))
      frame f-1:title-dcolor  = 8
      frame f-button:centered = true
      frame f-button:row      = 20.

END PROCEDURE. /* ip-framesetup */

PROCEDURE ip-button:

   define input-output parameter perform-status as character
      format "x(25)" no-undo.

   if not batchrun and c-application-mode <> "API"
   then do:

      enable all with frame f-button.
      ststatus = stline[1].
      status input ststatus.

      on choose of b-add
         do:
            assign
               perform-status = self:private-data
               l-focus        = self:handle.
            hide frame f-button no-pause.
         end. /* DO */

      on choose of b-undo
         do:
            assign
               perform-status = self:private-data
               l-focus        = self:handle
               l-title        = current-window:title + " - " + self:label.
            hide frame f-button no-pause.
         end. /* DO */

      on choose of b-end
         do:
            assign
               perform-status = self:private-data
               l-focus        = self:handle.
            hide frame f-1 no-pause.
            hide frame f-button no-pause.
         end. /* DO */

      on cursor-up, f9 anywhere
         do:
            assign
               perform-status = "prev"
               l-focus        = focus:handle.
            apply "go" to frame f-button.
         end. /* DO */

      on cursor-down, f10 anywhere
         do:
            assign
               perform-status = "next"
               l-focus        = focus:handle.
            apply "go" to frame f-button.
         end. /* DO */

      on cursor-right anywhere
         do:
            l-focus = self:handle.
            apply "tab" to l-focus.
         end. /* DO */

      on cursor-left anywhere
         do:
            l-focus = self:handle.
            if session:display-type = "gui"
            then
               apply "shift-tab" to l-focus.
            else
               apply "ctrl-u" to l-focus.
         end. /* DO */

      on f3 anywhere
         apply "choose" to b-add in frame f-button.
      on esc anywhere
         do:
            if session:display-type = "gui"
            then
               apply "choose" to b-end in frame f-button.
         end. /* DO */

      on end-error anywhere
         apply "choose" to b-end in frame f-button.
      on window-close of current-window
         apply "choose" to b-end in frame f-button.
      on go anywhere
         do:
            if (lastkey = keycode("cursor-down")
               or lastkey = keycode("cursor-up")
               or lastkey = keycode("f9")
               or lastkey = keycode("f10"))
            then
               return.
            else
               l-focus = focus:handle.
            apply "choose" to l-focus.
         end. /* DO */

      wait-for
         go of frame f-button or
         window-close of current-window or
         choose of
         b-add,
         b-undo,
         b-end
         focus
         l-focus.

   end. /* IF NOT batchrun */
   else if c-application-mode <> "API" then
         set perform-status.
   
   if c-application-mode <> "API" then
      hide message no-pause.

END PROCEDURE. /* PROCEDURE ip-button */


PROCEDURE ip_getcostdiff :
   define input        parameter i_asset  like faadj_fa_id   no-undo.
   define input        parameter i_book   like faadj_fabk_id no-undo.
   define input        parameter i_resrv  like faadj_resrv   no-undo.
   define input        parameter i_add    like mfc_logical   no-undo.
   define input-output parameter io_cost  like fa_puramt     no-undo.

   define buffer bFabk for fabk_mstr.

   for each fab_det
      where fab_det.fab_domain = global_domain 
      and  fab_fa_id   = i_asset
      and  fab_fabk_id = i_book
      and  fab_resrv   = i_resrv
      no-lock:
      /* ONLY POSTING BOOK NEED TO BE CALCULATED */
      for first bFabk no-lock
         where bFabk.fabk_domain = global_domain
         and bFabk.fabk_id = i_book
         and bFabk.fabk_post:
         if i_add
         then
            io_cost = io_cost + fab_amt.
         else
            io_cost = io_cost - fab_amt.
      end.
   end. /* FOR EACH fab_det */
END PROCEDURE. /* IP_GETCOSTDIFF */

/* TO DISPLAY ERROR MESSAGES ON INPUT OF faadj_yrper FIELD */
PROCEDURE p_err_messg:

   define buffer fabddet for fabd_det.
   for last fabd_det
      where fabd_det.fabd_domain = global_domain
      and   fabd_fa_id           = l-fa-id
      and   fabd_fabk_id         = l-fabk-id
      no-lock:
   end. /* FOR LAST fabd_det */
   if available fabd_det
      and fabd_suspend = no
   then do:
      /* CANNOT REINSTATE. ASSET NOT SUSPENDED */
      {us/bbi/pxmsg.i &MSGNUM=5969 &ERRORLEVEL=3}
      /* SET l-error FLAG TO UNDO,LEAVE */
      l-error = yes.
   end. /* IF AVAILABLE fabd_det */
   if l-error = no
   then do:
      for first fabd_det
         where fabd_det.fabd_domain = global_domain
         and   fabd_fa_id           = l-fa-id
         and   fabd_fabk_id         = l-fabk-id
         and fabd_yrper             = svFaadjYrPer
         no-lock:
      end. /* FOR FIRST fabd_det */
      if available fabd_det
      then do:
         for last last-faadj
            where last-faadj.faadj_domain  = global_domain
            and   last-faadj.faadj_fa_id   = l-fa-id
            and   last-faadj.faadj_fabk_id = l-fabk-id
            and   last-faadj.faadj_type    = "5"
            no-lock:
            if svFaadjYrPer <= last-faadj.faadj_yrper
            then do:
               /* REINSTATE DATE MUST BE LATER THAN SUSPEND DATE */
               {us/bbi/pxmsg.i &MSGNUM=3219 &ERRORLEVEL=3}
               l-error = yes.
            end. /* IF INPUT FRAME f-1 faadj_mstr.faadj_yrper ... */
         end. /* FOR LAST last-faadj */
      end. /* IF AVAILABLE fabd_det */
   end. /* IF l-error = no */
   if l-error
   then do:
      if c-application-mode <> "API" then 
      do:
         next-prompt faadj_mstr.faadj_yrper with frame f-1.
         next.
      end.
      else
         return.
   end. /* IF l-error */

   for first fabddet
      where fabddet.fabd_domain = global_domain 
      and   fabddet.fabd_fa_id     = l-fa-id
      and   fabddet.fabd_fabk_id   = l-fabk-id
      and   fabddet.fabd_transfer  = yes
      and   fabddet.fabd_suspend_transfer   = yes
      and   fabddet.fabd_yrper     > svFaadjYrPer
      no-lock:
      /* ASSET TRANSFERRED AND POSTED IN #. */
      /* REINSTATE IN A LATER PERIOD        */
      {us/bbi/pxmsg.i &MSGNUM=5971 &ERRORLEVEL=3
                      &MSGARG1=fabddet.fabd_yrper}
      l-error = yes.
   end. /* FOR FIRST fabddet */

END PROCEDURE. /* p_err_messg */
